//
//  RecruitmentDetailViewController.m
//  InviteJob
//
//  Created by LangLang on 17/10/26.
//  Copyright © 2017年 Shui. All rights reserved.
//

#import "RecruitmentDetailViewController.h"
#import "JobsChooseViewController.h"


@interface RecruitmentDetailViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>
{
    NSString *_cateId;
}
@property (nonatomic, strong) UIButton *navRightButton;

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@property (weak, nonatomic) IBOutlet UITextField *txJobsChoose;

@property (weak, nonatomic) IBOutlet UITextField *txDirection;


@property (weak, nonatomic) IBOutlet UITextField *txExperience;

@property (weak, nonatomic) IBOutlet UITextField *txRequirements;

@property (weak, nonatomic) IBOutlet UITextField *txKind;

@property (weak, nonatomic) IBOutlet UITextField *txWorkAddress;

@property (weak, nonatomic) IBOutlet UITextField *txInfortionAddress;

@property (weak, nonatomic) IBOutlet UIView *txMsg;

@property (weak, nonatomic) IBOutlet UITextField *TxTreatment;


@property (nonatomic,strong) SZTextView *textView;

@end

@implementation RecruitmentDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    TitleView(@"招聘信息")
    self.view.backgroundColor = strColor(@"f6f6f6");
    
    if (_zp_id) {
        [self getDataCompany];
    }
    
    // right item
    self.navRightButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
    [self.navRightButton setTitle:@"提交" forState:UIControlStateNormal];
    self.navRightButton.titleEdgeInsets = UIEdgeInsetsMake(0,10, 0, -10);
    self.navRightButton.titleLabel.font = [UIFont systemFontOfSize:16];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:self.navRightButton];
    self.navigationItem.rightBarButtonItem = rightItem;
    // 事件绑定
    [self.navRightButton addTarget:self action:@selector(navAction) forControlEvents:UIControlEventTouchUpInside];
    
    self.scrollView.contentSize = CGSizeMake(WIDTH, 477);
    
    
    
    // 岗位
    //注册通知(等待接收消息)
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(volue:) name:@"volue" object:nil];
   
    self.mem_province = @"";
    self.mem_city = @"";
    self.mem_country = @"";
    
    // picker
    self.pickerView = [[RegionPicker alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-64)];
    self.pickerView.pickerView.dataSource = self;
    self.pickerView.pickerView.delegate = self;
    [self.pickerView.confirmButton addTarget:self action:@selector(regionSelectAction) forControlEvents:UIControlEventTouchUpInside];
    [self.pickerView setHidden:YES];
    [self.view addSubview:self.pickerView];
   
    
    [_txDirection addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    
    _textView = [[SZTextView alloc] init];
    _textView.placeholder = @"请输入工作描述";
    _textView.backgroundColor = [UIColor whiteColor];
    _textView.font = [UIFont systemFontOfSize:15];
    [_txMsg addSubview:_textView];
    [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(@0);
    }];
    
    
}
- (void)textFieldDidChange: (UITextField *)sender {
    if (sender.text.length > 15) {
        sender.text = [sender.text substringToIndex:15];
    }
}
-(void)volue:(NSNotification *)sender{
    //打印通知传过来的数值
    _txJobsChoose.text = sender.userInfo[@"cate_name"];
    _cateId = sender.userInfo[@"cate_id"];
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)navAction {
    
    [self updata];
    
}



#pragma mark -- 岗位招聘  --
- (IBAction)jobsChoose:(id)sender {
    
    JobsChooseViewController *job = [JobsChooseViewController new];
    [self.navigationController pushViewController:job animated:YES];
    
}

#pragma mark -- 工作年限 --
- (IBAction)experience:(id)sender {
    
    NSArray * educationArray = @[@"不限",@"应届生",@"1年以内",@"1-3年",@"3-5年",@"5-10年",@"10年以上"];
    zySheetPickerView * pickerView = [zySheetPickerView ZYSheetStringPickerWithTitle:educationArray andHeadTitle:@"工作年限" Andcall:^(zySheetPickerView *pickerView, NSString *choiceString) {
        _txExperience.text = choiceString;
        [pickerView dismissPicker];
    }];
    [pickerView show];
    
}


#pragma mark -- 学业要求  --
- (IBAction)requirements:(id)sender {
    
    NSArray * educationArray = @[@"不限",@"初中",@"高中",@"中专",@"大专",@"本科",@"博士",@"硕士"];
    zySheetPickerView * pickerView = [zySheetPickerView ZYSheetStringPickerWithTitle:educationArray andHeadTitle:@"学历要求" Andcall:^(zySheetPickerView *pickerView, NSString *choiceString) {
        _txRequirements.text = choiceString;
        [pickerView dismissPicker];
    }];
    [pickerView show];
    
}


#pragma mark -- 职业类型  --
- (IBAction)kind:(id)sender {
    
    NSArray * educationArray = @[@"全职",@"兼职"];
    zySheetPickerView * pickerView = [zySheetPickerView ZYSheetStringPickerWithTitle:educationArray andHeadTitle:@"职位类型" Andcall:^(zySheetPickerView *pickerView, NSString *choiceString) {
        _txKind.text = choiceString;
        [pickerView dismissPicker];
    }];
    [pickerView show];
    
}


#pragma mark -- 工作地址  --
- (IBAction)workAddress:(id)sender {
    
    if(!self.province) {
        
        NSString *jsonPath = [[NSBundle mainBundle]pathForResource:@"region" ofType:@"json"];
        NSData *jsonData = [[NSData alloc]initWithContentsOfFile:jsonPath];
        self.province = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];
        self.city = self.province[0][@"city"];
        self.country = self.city[0][@"country"];
        self.mem_province = self.province[0][@"province_code"];
        self.mem_city = self.city[0][@"city_code"];
        self.mem_country = self.country[0][@"country_code"];
        self.txWorkAddress.text = [NSString stringWithFormat:@"%@%@%@",self.province[0][@"province_name"], self.city[0][@"city_name"], self.country[0][@"country_name"]];
        [self.pickerView setHidden:NO];
        [self.pickerView.pickerView reloadAllComponents];
    }else {
        [self.pickerView setHidden:NO];
        [self.pickerView.pickerView reloadAllComponents];
    }
    
}

#pragma mark -- 薪资待遇  --
- (IBAction)treatment:(id)sender {
    NSArray * educationArray = @[@"2k以下", @"2k-5k", @"5k-10k", @"10k-15k", @"15k-25k" ,@"面议"];
    zySheetPickerView * pickerView = [zySheetPickerView ZYSheetStringPickerWithTitle:educationArray andHeadTitle:@"薪资待遇" Andcall:^(zySheetPickerView *pickerView, NSString *choiceString) {
        _TxTreatment.text = choiceString;
        [pickerView dismissPicker];
    }];
    [pickerView show];
    
    
}

// 区域选择确定
- (void)regionSelectAction {
    [self.pickerView setHidden:YES];
    
    if (self.selectedRegion.length==0) {
        self.txWorkAddress.text = @"北京北京市东城区";
    }else{
       self.txWorkAddress.text = self.selectedRegion;
    }
    self.province_code = self.mem_province;
    self.city_code = self.mem_city;
    self.country_code = self.mem_country;
}

#pragma mark - UIPickerView datasource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 3;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    switch (component) {
        case 0:
        {
            if(![self.province isKindOfClass:[NSNull class]]) {
                return self.province.count;
            }else {
                return 0;
            }
        }
            break;
        case 1:
        {
            if(![self.city isKindOfClass:[NSNull class]]) {
                return self.city.count;
            }else {
                return 0;
            }
        }
            break;
        case 2:
        {
            if(![self.country isKindOfClass:[NSNull class]]) {
                return self.country.count;
            }else {
                return 0;
            }
        }
            break;
        default:
            break;
    }
    return 0;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    switch (component) {
            
        case 0:
        {
            // 省份
            return self.province[row][@"simple_province_name"];
        }
            break;
        case 1:
        {
            // 城市
            return self.city[row][@"simple_city_name"];
        }
            break;
        case 2:
        {
            // 乡镇、街道
            return self.country[row][@"simple_country_name"];
        }
            break;
        default:
            break;
    }
    return nil;
}

#pragma mark - UIPickerView delegate
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    switch (component) {
        case 0:
        {
            // 省份
            self.city = self.province[row][@"city"];
            self.country = self.city[0][@"country"];
            [pickerView selectRow:0 inComponent:1 animated:NO];
            [self pickerView:self.pickerView.pickerView didSelectRow:0 inComponent:1];
        }
            break;
        case 1:
        {
            // 城市
            self.country = self.city[row][@"country"];
            [pickerView selectRow:0 inComponent:2 animated:NO];
            [self pickerView:self.pickerView.pickerView didSelectRow:0 inComponent:2];
        }
            break;
        case 2:
        {
            // 乡镇、街道
        }
            break;
        default:
            break;
    }
    
    
    self.mem_province = self.province[[pickerView selectedRowInComponent:0]][@"province_code"];
    self.selectedRegion = self.province[[pickerView selectedRowInComponent:0]][@"province_name"];
    
    self.mem_city = self.city[[pickerView selectedRowInComponent:1]][@"city_code"];
    self.selectedRegion = [self.selectedRegion stringByAppendingString:self.city[[pickerView selectedRowInComponent:1]][@"city_name"]];
    
    if([self.country isKindOfClass:[NSArray class]]) {
        self.mem_country = self.country[[pickerView selectedRowInComponent:2]][@"country_code"];
        self.selectedRegion = [self.selectedRegion stringByAppendingString:self.country[[pickerView selectedRowInComponent:2]][@"country_name"]];
    }else {
        self.mem_country = @"1";
    }
    
    [pickerView reloadAllComponents];
}



#pragma mark --数据上传

-(void)updata{
    
    if (_txJobsChoose.text.length==0) {
        SHuiMBhudTextTimeStr(@"请选择招聘岗位");
        return;
    }
    if (_txDirection.text.length==0) {
        _txDirection.text = @"";
    }
    if (_txExperience.text.length==0) {
        SHuiMBhudTextTimeStr(@"请选择工作年限");
        return;
    }
    if (_txRequirements.text.length==0) {
        SHuiMBhudTextTimeStr(@"请选择学业要求");
        return;
    }
    if (_txKind.text.length==0) {
        SHuiMBhudTextTimeStr(@"请选择职位类型");
        return;
    }
    if (_txWorkAddress.text.length==0) {
        SHuiMBhudTextTimeStr(@"请选择工作地址");
        return;
    }
    if (_txInfortionAddress.text.length==0) {
        SHuiMBhudTextTimeStr(@"请填写详细地址");
        return;
    }
    if (_TxTreatment.text.length==0) {
        SHuiMBhudTextTimeStr(@"请选择工资待遇");
        return;
    }
    if (_textView.text.length==0) {
        SHuiMBhudTextTimeStr(@"请输入工作描述");
        return;
    }
    
    UIAlertController *actionSheetController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"确定提交吗？"  preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *determineAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
        isUser
        NSString *strUrl;
        NSDictionary *parameter;
        
        if (_zp_id) {
            strUrl = [NSString stringWithFormat:post_editEmploy_api];
            parameter = @{@"user_id":user.user_id,
                          @"token":user.token,
                          @"zp_id":_zp_id,
                          @"cate_id":_cateId,
                          @"direction":_txDirection.text,
                          @"province":_province_code,
                          @"city":_city_code,
                          @"county":_country_code,
                          @"area":_txInfortionAddress.text,
                          @"work_experience":_txExperience.text,
                          @"education":_txRequirements.text,
                          @"salary":_TxTreatment.text,
                          @"content":_textView.text,
                          @"pos_type":_txKind.text};
        }else{
            strUrl = [NSString stringWithFormat:post_Employ_publishEmploy_api];
            parameter = @{@"user_id":user.user_id,
                          @"token":user.token,
                          @"cate_id":_cateId,
                          @"direction":_txDirection.text,
                          @"province":_province_code,
                          @"city":_city_code,
                          @"county":_country_code,
                          @"area":_txInfortionAddress.text,
                          @"work_experience":_txExperience.text,
                          @"education":_txRequirements.text,
                          @"salary":_TxTreatment.text,
                          @"content":_textView.text,
                          @"pos_type":_txKind.text};
        }
        
        [PostNetWorking postNetWorkingStr:strUrl andparameters:parameter Success:^(id responseObject) {
            int code = [responseObject[@"code"]intValue];
            switch (code) {
                case 200:
                {
                    SHuiMBhudTextTimeStr(@"提交成功");
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                        [self.navigationController popViewControllerAnimated:YES];
                    });
                }
                    break;
                case 210:
                {
                    //逻辑错误
                    [MBProgressHUD showMessag:responseObject[@"mark"] withDelay:0 toView:self.view withMode:MBProgressHUDModeText hideWithDelay:1.5];
                }
                    break;
                case 220:
                {
                    //token过期
                    [MBProgressHUD showMessag:@"登录状态已过期，请重新登录" withDelay:0 toView:self.view withMode:MBProgressHUDModeText hideWithDelay:1.5];
                }
                default:
                    break;
            }
            
        } failed:^(NSError *error) {
            [MBProgressHUD showMessag:@"网络错误" withDelay:0 toView:self.view withMode:MBProgressHUDModeText hideWithDelay:1.5];
        }];
        
    }];
    
    [actionSheetController addAction:determineAction];
    [actionSheetController addAction:cancelAction];
    
    [self presentViewController:actionSheetController animated:YES completion:nil];
    
    
    
}


#pragma mark --  获取数据

-(void)getDataCompany{
    isUser
    NSString *strUrl = [NSString stringWithFormat:post_getEmployInfo_api];
    NSDictionary *parameter = @{@"user_id":user.user_id,
                                @"token":user.token,
                                @"zp_id":_zp_id};
    [PostNetWorking postNetWorkingStr:strUrl andparameters:parameter Success:^(id responseObject) {
        int code = [responseObject[@"code"]intValue];
        switch (code) {
            case 200:
            {
                
                _txJobsChoose.text = responseObject[@"datas"][@"cate_name"];
                _cateId = responseObject[@"datas"][@"cate_id"];
                _txDirection.text = responseObject[@"datas"][@"direction"];
                _txExperience.text = responseObject[@"datas"][@"work_experience"];
                _txRequirements.text = responseObject[@"datas"][@"education"];
                 _txKind.text = responseObject[@"datas"][@"pos_type"];
                 _txInfortionAddress.text = responseObject[@"datas"][@"area"];
                
                _txWorkAddress.text = [NSString stringWithFormat:@"%@%@%@",responseObject[@"datas"][@"province_name"],responseObject[@"datas"][@"city_name"],responseObject[@"datas"][@"county_name"]];
                _TxTreatment.text = responseObject[@"datas"][@"salary"];
                _textView.text = responseObject[@"datas"][@"content"];
                _country_code = responseObject[@"datas"][@"county"];
                _city_code = responseObject[@"datas"][@"city"];
                _province_code = responseObject[@"datas"][@"province"];
                
            }
                break;
            case 210:
            {
                //逻辑错误
                [MBProgressHUD showMessag:responseObject[@"mark"] withDelay:0 toView:self.view withMode:MBProgressHUDModeText hideWithDelay:1.5];
            }
                break;
            case 220:
            {
                //token过期
                [MBProgressHUD showMessag:@"登录状态已过期，请重新登录" withDelay:0 toView:self.view withMode:MBProgressHUDModeText hideWithDelay:1.5];
            }
            default:
                break;
        }
        
    } failed:^(NSError *error) {
        [MBProgressHUD showMessag:@"网络错误" withDelay:0 toView:self.view withMode:MBProgressHUDModeText hideWithDelay:1.5];
    }];
}




@end
